package com.example.myapplication;

import android.content.Intent;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.util.MyHelper;
import com.example.myapplication.util.saveSoul;

public class DB1 extends AppCompatActivity {

    private EditText usernameEd;


    private EditText passwordEd;
    private Button Nowlogin;
    private String usernameStr;
    private String passwordStr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginnow_activity);
        initView();
        initData();
        initEvent();
    }
    private void initView(){
        usernameEd = findViewById(R.id.user_et_login);
        passwordEd = findViewById(R.id.password_et_login);
        Nowlogin = findViewById(R.id.login_btn_login);
    } private void initData(){

        usernameStr = usernameEd.getText().toString()+"";
        passwordStr = passwordEd.getText().toString()+"";
        //实例化SharedPreferences对象
//        SharedPreferences sharedPreferences =getSharedPreferences("data",MODE_PRIVATE);
//        //开启编辑器
//        SharedPreferences.Editor editor=sharedPreferences.edit();
//        //向编辑器存入数据
//        editor.putString("username",usernameStr);
//        editor.putString("password",passwordStr);
//        //提交数据
//        editor.commit();
//        saveSoul.save(DB1.this,"usrname",usernameStr);
//        saveSoul.save(DB1.this,"password",passwordStr);
//        MyHelper myHelper =new MyHelper(DB1.this);
//        myHelper.insert(DB1.this,usernameStr,passwordStr);
    } private void initEvent(){

       Nowlogin.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               initData();
               if (usernameStr == "" || passwordStr == ""){
                   Toast.makeText(DB1.this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
               }else{
                   saveSoul saveSoul = new saveSoul(DB1.this);
                   Boolean a = saveSoul.getSaveString(usernameStr,passwordStr);
                   if (a) {
                       Intent intent = new Intent(DB1.this, LoginPage.class);
                       startActivity(intent);
                   }else {
                       Toast.makeText(DB1.this, "注册成功，请登录", Toast.LENGTH_SHORT).show();
                   }

               }



           }
       });
            }


}